# 使用免费天气API
# 使用说明参考：https://www.sojson.com/api/weather.html

import requests,json
import re

def getWeather(cityid):
    result = {};
    url = "http://t.weather.sojson.com/api/weather/city/"+str(cityid);
    r = requests.get(url)
    # print(r.text)
    weaterData = json.loads(r.text)
    
    if weaterData['status'] == 200 :
        # 昨天天气
        yesterday = weaterData['data']['yesterday']
    
        # 今天天气
        today = weaterData['data']['forecast'][0]

        result['city'] = weaterData['cityInfo']['updateTime']
        result['update'] = weaterData['cityInfo']['updateTime']
        result['todayType'] = today['type']
        result['todayLow'] = int(re.findall(r"-?\d+\.?\d*", today['low'])[0])
        result['todayHigh'] = int(re.findall(r"-?\d+\.?\d*", today['high'])[0])
        result['subYesterdayLow'] = int(result['todayLow']) - int(re.findall(r"-?\d+\.?\d*", yesterday['low'])[0])
        result['subYesterdayHigh'] = int(result['todayHigh']) - int(re.findall(r"-?\d+\.?\d*", yesterday['high'])[0])

        return result

    else:
        return False

get = getWeather(101210106)

print(get)

